package com.pos;

import java.util.List;

public class PosService {

	private double calSumPrice(List<Book> books) {
		double sumPrice = 0;
		for (Book book : books) {
			sumPrice += book.getPrice();
		}
		return sumPrice;
	}

	public double payBooks(Cart cart, double givenMoney, double discount) {
		double sumPrice = calSumPrice(cart.getBooks());
		return givenMoney - sumPrice * discount;
	}

	public double payBooks(Cart cart, double givenMoney, Customer c) {
		double sumPrice = calSumPrice(cart.getBooks());
		double needPay = sumPrice * getDiscountByLevel(c.getLevel());
		c.setPoint(c.getPoint() + (int) needPay);
		if (c.getPoint() >= 1000) {
			c.setLevel("PLATINUM");
		}
		if (c.getPoint() >= 3000) {
			c.setLevel("GOLD");
		}
		if (c.getPoint() >= 5000) {
			c.setLevel("DIAMOND");
		}
		return givenMoney - needPay;
	}

	private double getDiscountByLevel(String level) {
		if ("COMMON".equals(level)) {
			return 1.0;
		} else if ("PLATINUM".equals(level)) {
			return 0.95;
		} else if ("GOLD".equals(level)) {
			return 0.8;
		} else if ("DIAMOND".equals(level)) {
			return 0.6;
		}
		return 1.0;
	}

}
